package com.xiaoshuidi.cloud.module.bpm.controller.admin.reserveticket;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshuidi.cloud.framework.security.core.LoginUser;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.bpm.controller.app.reserveticket.vo.RemainingReserveQuantityReqVO;
import com.xiaoshuidi.cloud.module.bpm.convert.reserveticket.MyReserveTicketConvert;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;

import com.xiaoshuidi.cloud.framework.operatelog.core.annotations.OperateLog;
import static com.xiaoshuidi.cloud.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.xiaoshuidi.cloud.module.bpm.controller.admin.reserveticket.vo.*;
import com.xiaoshuidi.cloud.module.bpm.dal.dataobject.reserveticket.ReserveTicketDO;
import com.xiaoshuidi.cloud.module.bpm.convert.reserveticket.ReserveTicketConvert;
import com.xiaoshuidi.cloud.module.bpm.service.reserveticket.ReserveTicketService;

@Tag(name = "管理后台 - 预约工单")
@RestController
@RequestMapping("/bpm/reserve-ticket")
@Validated
public class ReserveTicketController {
    @Resource
    private ReserveTicketService reserveTicketService;

    @PostMapping("/create")
    @Operation(summary = "创建预约工单")
    @PreAuthorize("@ss.hasPermission('bpm:reserve-ticket:create')")
    public CommonResult<Long> createReserveTicket(@Valid @RequestBody ReserveTicketCreateReqVO createReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        return success(reserveTicketService.createReserveTicket(userId, createReqVO, 1));
    }

    @PostMapping("/update")
    @Operation(summary = "更新预约工单")
    @PreAuthorize("@ss.hasPermission('bpm:reserve-ticket:update')")
    public CommonResult<Boolean> updateReserveTicket(@Valid @RequestBody ReserveTicketUpdateReqVO updateReqVO, HttpServletRequest request) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        reserveTicketService.updateReserveTicket(userId, updateReqVO, request);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除预约工单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bpm:reserve-ticket:delete')")
    public CommonResult<Boolean> deleteReserveTicket(@RequestParam("id") Long id, HttpServletRequest request) {
        reserveTicketService.deleteReserveTicket(id, request);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得预约工单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bpm:reserve-ticket:query')")
    public CommonResult<GetReserveTicketRespVO> getReserveTicket(@RequestParam("id") Long id, HttpServletRequest request) {
        return success(reserveTicketService.getReserveTicket(id, request));
    }

    @GetMapping("/list")
    @Operation(summary = "获得预约工单列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('bpm:reserve-ticket:query')")
    public CommonResult<List<ReserveTicketRespVO>> getReserveTicketList(@RequestParam("ids") Collection<Long> ids) {
        List<ReserveTicketDO> list = reserveTicketService.getReserveTicketList(ids);
        return success(ReserveTicketConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得预约工单分页")
    @PreAuthorize("@ss.hasPermission('bpm:reserve-ticket:query')")
    public CommonResult<PageResult<ReserveTicketRespVO>> getReserveTicketPage(ReserveTicketPageReqVO pageVO, HttpServletRequest request) {
        return success(reserveTicketService.getReserveTicketPage(pageVO, request));
    }

    @GetMapping("/status-count")
    @Operation(summary = "分页状态数量统计")
    @PreAuthorize("@ss.hasPermission('bpm:reserve-ticket:query')")
    public CommonResult<StatusCountRespVO> statusCount(@RequestParam("status") Integer status, HttpServletRequest request) {
        return success(reserveTicketService.statusCount(status, request));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出预约工单 Excel")
    @PreAuthorize("@ss.hasPermission('bpm:reserve-ticket:export')")
    public void exportReserveTicketExcel(@Valid ReserveTicketExportReqVO exportReqVO,
              HttpServletResponse response, HttpServletRequest request) throws IOException {
        List<ReserveTicketRespVO> list = reserveTicketService.getReserveTicketList(exportReqVO, request);
        // 导出 Excel
        List<ReserveTicketExcelVO> datas = MyReserveTicketConvert.convert(list);
        ExcelUtils.write(response, "预约工单.xls", "数据", ReserveTicketExcelVO.class, datas);
    }

    @PostMapping("/assignment")
    @Operation(summary = "分配跟进人")
    @PreAuthorize("@ss.hasPermission('bpm:reserve-ticket:update')")
    public CommonResult<Long> assignment(@RequestBody @Valid AssignmentReqVO assignmentReqVO, HttpServletRequest request){
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        return success(reserveTicketService.assignment(loginUser, assignmentReqVO, request));
    }

    @PostMapping("/modify-progress")
    @Operation(summary = "修改进度")
    @PreAuthorize("@ss.hasPermission('bpm:reserve-ticket:update')")
    public CommonResult<Boolean> modifyProgress(@RequestBody @Valid ModifyProgressReqVO modifyProgressReqVO){
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        return success(reserveTicketService.modifyProgress(userId, modifyProgressReqVO));
    }

    @GetMapping("/get-progress-enum")
    @Operation(summary = "获取进度枚举")
    @PreAuthorize("@ss.hasPermission('bpm:reserve-ticket:query')")
    public CommonResult<List<GetProgressEnumRespVO>> getProgressEnum(){
        return success(reserveTicketService.getProgressEnum());
    }

    @GetMapping("/count-total-number")
    @Operation(summary = "工作台-待处理工单统计接口")
    @PreAuthorize("@ss.hasPermission('bpm:reserve-ticket:query')")
    public CommonResult<GetTotalNumberRespVO> getTotalNumber(){
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        return success(reserveTicketService.getTotalNumber(userId));
    }

    @GetMapping("/personal-reserve-page")
    @Operation(summary = "个人中心-预约工单列表")
    @Parameter(name = "mobile", description = "个人用户手机号")
    @PreAuthorize("@ss.hasPermission('bpm:reserve-ticket:query')")
    public CommonResult<PageResult<PersonalReserveRespVO>> personalReservePage(@RequestParam("mobile") String mobile, Integer pageSize, Integer pageNo){
        return success(reserveTicketService.personalReservePage(mobile, pageSize, pageNo));
    }

    @GetMapping("/reserve-urgent-repair-num")
    @Operation(summary = "管家端 - 预约看房 和 紧急报修数量")
    @PreAuthorize("@ss.hasPermission('bpm:reserve-ticket:query')")
    public CommonResult<ReserveUrgentRepariNumRespVO> reserveUrgentRepariNum(){
        return success(reserveTicketService.reserveUrgentRepariNum());
    }

    @GetMapping("/remaining-reserve-quantity")
    @Operation(summary = "获取各时间段下可租数量")
    @PreAuthorize("@ss.hasPermission('bpm:reserve-ticket:query')")
    public CommonResult<Map<Integer, Integer>> remainingReserveQuantity(RemainingReserveQuantityReqVO reqVO){
        return success(reserveTicketService.remainingReserveQuantity(reqVO.getApartmentId(), reqVO.getReserveDate()));
    }
}
